<?php
/********************************************************************
Product    : Plotalot
Date       : 15 April 2011
Copyright  : Les Arbres Design 2010-2011
Contact    : http://extensions.lesarbresdesign.info
Licence    : GNU General Public License
*********************************************************************/

defined('_JEXEC') or die('Restricted Access');

jimport('joomla.application.component.controller');

class PlotalotController extends JController
{
	function __construct()
	{
		parent::__construct();						// automatically maps public functions
		$this->registerTask('apply', 'save');
	}

	function display()
	{
		$model = $this->getModel('chart');
		$chart_list = $model->getList();
		$pagination = $model->getPagination();
		
		$view = $this->getView('chartlist', 'html');
		$view->assignRef('items', $chart_list);
		$view->assignRef('pagination', $pagination);
		$view->display();
	}
	
	function new_chart()
	{
		JRequest::setVar('hidemainmenu', 1);
		$model = $this->getModel('chart');
		$data = $model->initData(CHART_TYPE_LINE);
		
		$view = $this->getView('chart','html');
		$view->assignRef('chart_data', $data);
		$view->display();
	}

	function new_table()
	{
		JRequest::setVar('hidemainmenu', 1);
		$model = $this->getModel('chart');
		$data = $model->initData(CHART_TYPE_TABLE);

		$view = $this->getView('table','html');
		$view->assignRef('chart_data', $data);
		$view->display();
	}

	function new_item()
	{
		JRequest::setVar('hidemainmenu', 1);
		$model = $this->getModel('chart');
		$data = $model->initData(CHART_TYPE_SINGLE_ITEM);

		$view = $this->getView('item','html');
		$view->assignRef('chart_data', $data);
		$view->display();
	}

	function edit()
	{
		JRequest::setVar('hidemainmenu', 1);
		$model = $this->getModel('chart');
		$cid = JRequest::getVar('cid', 0, '', 'array');
		$id = (int) $cid[0];
		$data = $model->getOne($id);
		if ($data === false)
			{												// an error has been enqueued
			$this->setRedirect(LA_COMPONENT_LINK);			// redirect back to list
			return;
			}
			
		switch ($data->chart_type)
			{
			case CHART_TYPE_TABLE:
				$view = $this->getView('table','html');
				break;
			case CHART_TYPE_SINGLE_ITEM:
				$view = $this->getView('item','html');
				break;
			default:
				$view = $this->getView('chart','html');
			}

		$view->assignRef('chart_data', $data);
		$view->display();
	}
	
	function save()
	{
		JRequest::checkToken() or jexit('Invalid Token');
		$task = JRequest::getVar('task');			// 'save' or 'apply'
		$model = $this->getModel('chart');
		$stored = $model->store();					// does validation and may enqueue error messages
		
		if ($stored and ($task == 'save'))
			{
			$msg = JText::_('COM_PLOTALOT_CHART_SAVED');
			$this->setRedirect(LA_COMPONENT_LINK, $msg);
			return;
			}
			
		// apply or save failure (db error or validation failure)
		// errors may have been enqueued.

		switch ($model->_data->chart_type)
			{
			case CHART_TYPE_TABLE:
				$view = $this->getView('table','html');
				break;
			case CHART_TYPE_SINGLE_ITEM:
				$view = $this->getView('item','html');
				break;
			default:
				$view = $this->getView('chart','html');
			}

		$view->assignRef('chart_data', $model->_data);
		$view->display();
	}

	function copy()					// make a copy of one chart and edit it
	{
		JRequest::checkToken() or jexit('Invalid Token');
		JRequest::setVar('hidemainmenu', 1);
		$model = $this->getModel('chart');
		$cid = JRequest::getVar('cid', 0, '', 'array');
		$id = (int) $cid[0];
		$data = $model->getOne($id);
		if ($data === false)
			{												// an error has been enqueued
			$this->setRedirect(LA_COMPONENT_LINK);			// redirect back to list
			return;
			}
			
		switch ($data->chart_type)
			{
			case CHART_TYPE_TABLE:
				$view = $this->getView('table','html');
				break;
			case CHART_TYPE_SINGLE_ITEM:
				$view = $this->getView('item','html');
				break;
			default:
				$view = $this->getView('chart','html');
			}

		$data->id = 0;									// force creation of a new record
		$data->chart_name = '';							// force user to choose a new name
		$view->assignRef('chart_data', $data);
		$view->display();
	}

	function remove()
	{
		JRequest::checkToken() or jexit('Invalid Token');
		$model = $this->getModel('chart');
		if ($model->delete())
			$msg = JText::_('COM_PLOTALOT_DELETED');
		$this->setRedirect(LA_COMPONENT_LINK, $msg);
	}

	function publish()
	{
		JRequest::checkToken() or jexit('Invalid Token');
		$model = $this->getModel('chart');
		$model->publish(1);
		$this->setRedirect(LA_COMPONENT_LINK, $msg);
	}

	function unpublish()
	{
		JRequest::checkToken() or jexit('Invalid Token');
		$model = $this->getModel('chart');
		$model->publish(0);
		$this->setRedirect(LA_COMPONENT_LINK, $msg);
	}
	
	function help()
	{
		JRequest::checkToken() or jexit('Invalid Token');
		$view = $this->getView('help','html');
		$view->display();
	}
		
}
